import QtQuick 2.0
import RabbitMqClient 1.0

Item {
    QtObject {
        id: id_internal
        property int subscribeId: 0
        property string uri: "amqp://icsmq:icsmq@10.68.41.45:5672/"
    }
    function publish(servicename, msg, callback) {
        var reqId = 0
        if (servicename === "procedure") {
            reqId = rmq_procedure_client.request(msg)
            rmq_procedure_client.req_callbacks[reqId] = callback
        } else if (servicename === "storage") {
            reqId = rmq_storage_client.request(msg)
            rmq_storage_client.req_callbacks[reqId] = callback
        } else {
            console.warn("unknown service name:", servicename)
        }
    }
    function subscribe(servicename, callback) {
        var sid = id_internal.subscribeId++
        if (servicename === "procedure") {
            rmq_procedure_client.sub_callbacks[sid] = callback
        } else if (servicename === "storage") {
            rmq_storage_client.sub_callbacks[sid] = callback
        } else {
            console.warn("can not find the subscribe servicename:", servicename)
            return 0
        }
        return sid
    }

    function unsubscribe(sid) {
        if (rmq_storage_client.sub_callbacks.hasOwnProperty(sid)) {
            console.debug("unsubscribe procedure subscribe id:", sid)
            delete rmq_storage_client.sub_callbacks[sid]
        } else if (rmq_procedure_client.sub_callbacks.hasOwnProperty(sid)) {
            console.debug("unsubscribe storage subscribe id:", sid)
            delete rmq_procedure_client.sub_callbacks[sid]
        } else {
            console.warn("can not find the subscribe id:", sid)
        }
    }

    RabbitMqClient {
        id: rmq_storage_client
        servicename: "storage"
        uri: id_internal.uri
        property var req_callbacks: ({})
        property var sub_callbacks: ({})
        onSubscribedMsg: function (msg) {
            try {
                for (var key in sub_callbacks) {
                    sub_callbacks[key](msg)
                }
            } catch (e) {
                console.warn(e)
            }
        }
        onResponseMsg: function (reqId, msg) {
            console.debug("onResponseMsg reqId:" + reqId + " msg:" + msg)
            try {
                if (req_callbacks.hasOwnProperty(reqId)) {
                    req_callbacks[reqId](msg)
                    delete req_callbacks[reqId]
                }
            } catch (e) {
                console.warn(e)
            }
        }
        Component.onCompleted: {
            rmq_storage_client.start()
        }
    }
    RabbitMqClient {
        id: rmq_procedure_client
        servicename: "procedure"
        uri: id_internal.uri
        property var req_callbacks: ({})
        property var sub_callbacks: ({})
        onSubscribedMsg: function (msg) {
            try {
                for (var key in sub_callbacks) {
                    sub_callbacks[key](msg)
                }
            } catch (e) {
                console.warn(e)
            }
        }
        onResponseMsg: function (reqId, msg) {
            try {
                if (req_callbacks.hasOwnProperty(reqId)) {
                    req_callbacks[reqId](msg)
                    delete req_callbacks[reqId]
                }
            } catch (e) {
                console.warn(e)
            }
        }
        Component.onCompleted: {
            rmq_procedure_client.start()
        }
    }
}
